package com.gree.scmservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.gree.framework.entity.CusUserDetails;
import com.gree.framework.entity.SysUser;
import com.gree.framework.utils.SecurityUtils;
import com.gree.scmservice.dao.RoleDao;
import com.gree.scmservice.entity.smbse.Smbse05;
import com.gree.scmservice.entity.smbse.Smbse08;
import com.gree.scmservice.entity.smbse.SysRoleMo;
import com.gree.scmservice.service.GscmRoleService;
import org.nutz.dao.Dao;
import org.nutz.dao.pager.Pager;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 180686 on 2021/3/16 20:26
 */

@Service
public class GscmRoleServiceImpl implements GscmRoleService {

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private Dao dao;

    @Autowired
    DataSourceTransactionManager dataSourceTransactionManager;

    @Autowired
    TransactionDefinition transactionDefinition;

    /**
     * 分页查询
     * @param jsonObject 角色信息
     * @return
     */
    @Override
    public NutMap selectRoleList(JSONObject jsonObject) {

        CusUserDetails cusUserDetails = SecurityUtils.getUser();
        SysUser sysUser = cusUserDetails.getUser();
        String comp = sysUser.getComp();
        String filter = "where 1 = 1 and sb05.pano = 'MENU'";

        if(!"".equals(jsonObject.getString("roleName")) && jsonObject.getString("roleName")!=null){
            filter+=" and sb05.dsca like '%"+jsonObject.getString("roleName")+"%'";
        }
        if(!"".equals(jsonObject.getString("roleKey")) && jsonObject.getString("roleKey")!=null){
            filter+=" and sb05.roid like '%"+jsonObject.getString("roleKey")+"%'";
        }
        if(!"".equals(jsonObject.getString("styp")) && jsonObject.getString("styp")!=null){
            filter+=" and sb05.styp = '"+jsonObject.getString("styp")+"'";
        }
        if(!"".equals(jsonObject.getString("pano")) && jsonObject.getString("pano")!=null){
            filter+=" and sb05.pano = '"+jsonObject.getString("pano")+"'";
        }

        int count = roleDao.getRoleCount(comp,filter);
        Pager pager = dao.createPager(jsonObject.getIntValue("page"),jsonObject.getIntValue("pagesize"));
        List<Smbse05> dataList = roleDao.displayRole(comp,filter,count,pager);
        List<SysRoleMo> roleMos = new ArrayList<>();
        for(Smbse05 smbse05:dataList){
            roleMos.add(smbse05.toSysRoleMo());
        }
        return NutMap.NEW().addv("dataList",roleMos).addv("page",pager);
    }

    /**
     * roid查询角色信息
     * @param roleKey
     * @return
     */
    @Override
    public SysRoleMo selectRoleByRoleKey(String roleKey) {
        CusUserDetails cusUserDetails = SecurityUtils.getUser();
        SysUser sysUser = cusUserDetails.getUser();
        String comp = sysUser.getComp();
        Smbse05 smbse05 = roleDao.fetchSmbse05ById(comp,roleKey);
        return smbse05.toSysRoleMo();
    }

    /**
     * 校验角色权限是否唯一
     * @param sysRoleMo 角色信息
     * @return
     */
    @Override
    public boolean checkRoleKeyUnique(SysRoleMo sysRoleMo) {
        Smbse05 smbse05 = roleDao.fetchSmbse05ById("400",sysRoleMo.getRoleKey());
        return smbse05 == null?false:true;
    }

    /**
     * 插入
     * @param sysRoleMo 角色信息
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insertRole(SysRoleMo sysRoleMo) {
        CusUserDetails cusUserDetails = SecurityUtils.getUser();
        SysUser sysUser = cusUserDetails.getUser();
        String comp = sysUser.getComp();
        Smbse05 smbse05 = sysRoleMo.toSmbse05();
        List<Smbse05> list = new ArrayList<>();
        list.add(smbse05);
        TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
        try{
            List<Smbse05> smbse05List = roleDao.insertSmbse05(comp,list);

            Long[] menuIds = sysRoleMo.getMenuIds();
            String roleKey = sysRoleMo.getRoleKey();
            ArrayList<Smbse08> smbse08s = new ArrayList<Smbse08>();
            for(Long menuId:menuIds){
                Smbse08 smbse08 = new Smbse08(menuId,roleKey,"");
                smbse08s.add(smbse08);
            }
            List<Smbse08> smbse08List = roleDao.insertSmbse08s(comp,smbse08s);
            dataSourceTransactionManager.commit(transactionStatus);
            return smbse05List.size() + smbse08List.size();
        }catch (Exception e) {
            dataSourceTransactionManager.rollback(transactionStatus);
            return 0;
        }
    }

    /**
     * 更新
     * @param sysRoleMo 角色信息
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int updateRole(SysRoleMo sysRoleMo) {
        CusUserDetails cusUserDetails = SecurityUtils.getUser();
        SysUser sysUser = cusUserDetails.getUser();
        String comp = sysUser.getComp();
        Smbse05 smbse05 = sysRoleMo.toSmbse05();
        List<Smbse05> list = new ArrayList<>();
        list.add(smbse05);
        String roleKey = sysRoleMo.getRoleKey();
        TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
        try{
            int result = roleDao.updateSmbse05(comp,list);
            result += roleDao.deleteSmbse08sByRoids(comp,new String[]{roleKey});
            Long[] menuIds = sysRoleMo.getMenuIds();
            ArrayList<Smbse08> smbse08s = new ArrayList<Smbse08>();
            for(Long menuId:menuIds){
                Smbse08 smbse08 = new Smbse08(menuId,roleKey,"");
                smbse08s.add(smbse08);
            }
            List<Smbse08> smbse08List = roleDao.insertSmbse08s(comp,smbse08s);
            dataSourceTransactionManager.commit(transactionStatus);
            return result+smbse08List.size();
        }catch (Exception e) {
            dataSourceTransactionManager.rollback(transactionStatus);
            return 0;
        }
    }

    /**
     * 删除
     * @param roleIds 需要删除的角色ID
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int deleteRoleByIds(String[] roleIds){
        CusUserDetails cusUserDetails = SecurityUtils.getUser();
        SysUser sysUser = cusUserDetails.getUser();
        String comp = sysUser.getComp();
        TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
        try{
            int result = roleDao.deleteSmbse05ByRoleKeys(comp,roleIds);
            result += roleDao.deleteSmbse08sByRoids(comp,roleIds);
            dataSourceTransactionManager.commit(transactionStatus);
            return result;
        }catch (Exception e) {
            dataSourceTransactionManager.rollback(transactionStatus);
            return 0;
        }
    }
}
